package com.example.zhu95.mytimber.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.utils.Util;

import java.util.List;

import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Play;

/**
 * @author zhu95
 * @date 2018/3/24 14:14
 */

public class SimpleSongAlbumAdapter extends RecyclerView.Adapter<SimpleSongAlbumAdapter.ViewHolder> {
    List<SongBean> songs;

    public SimpleSongAlbumAdapter(List<SongBean> songs) {
        this.songs = songs;
    }

    public void setSongs(List<SongBean> songs) {
        this.songs = songs;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_simple_album, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final SongBean song = songs.get(position);
        Glide.with(holder.itemView.getContext()).
                load(song.getAlbumArt())
                .error(R.drawable.ic_empty_music2).
                into(holder.imgAlbum);
        holder.imgAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.sendPlayMusicCmdBroadcast(song.getId(), Util.INSTANCE.getIdsStrFormat(songs), PlayMusicCmd_Play);
            }
        });
    }

    @Override
    public int getItemCount() {
        return songs.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imgAlbum;

        public ViewHolder(View itemView) {
            super(itemView);
            imgAlbum = itemView.findViewById(R.id.img_simple_album);
        }
    }
}
